// src/main/java/com/bookmanager/controller/DashboardController.java
package com.bookmanager.controller;

import com.bookmanager.model.BorrowRecord;
import com.bookmanager.model.Favorite;
import com.bookmanager.model.User;
import com.bookmanager.service.FavoriteService;
import com.bookmanager.service.StatisticsService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/dashboard")
public class DashboardController extends HttpServlet {
    private StatisticsService statisticsService = new StatisticsService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        StatisticsService statisticsService = new StatisticsService();
        // 根据用户角色设置统计数据
        if ("admin".equals(user.getRole())) {
            // 管理员数据
            request.setAttribute("totalBooks", statisticsService.getTotalBooks());
            request.setAttribute("totalUsers", statisticsService.getTotalUsers());
            request.setAttribute("todayBorrows", statisticsService.getTodayBorrows());
            request.setAttribute("overdueBooks", statisticsService.getOverdueBooks());

            // 转发到管理员仪表板
            request.getRequestDispatcher("admin/dashboard.jsp").forward(request, response);
        } else {
            // 普通用户数据
            request.setAttribute("currentBorrows", statisticsService.getCurrentBorrows(user.getUserId()));
            request.setAttribute("dueSoonBooks", statisticsService.getDueSoonBooks(user.getUserId()));
            request.setAttribute("borrowHistory", statisticsService.getBorrowHistory(user.getUserId()));

            // 获取真实的收藏数量
            FavoriteService favoriteService = new FavoriteService();
            List<Favorite> favorites = favoriteService.getUserFavorites(user.getUserId());
            request.setAttribute("favoriteBooks", favorites != null ? favorites.size() : 0);

            // 为当前借阅记录添加收藏状态
            List<BorrowRecord> currentBorrowRecords = statisticsService.getCurrentBorrowRecords(user.getUserId());
            Map<Integer, Boolean> borrowRecordFavoriteStatus = new HashMap<>();

            if (currentBorrowRecords != null) {
                for (BorrowRecord record : currentBorrowRecords) {
                    boolean isFavorite = favoriteService.isBookFavorite(user.getUserId(), record.getBookId());
                    borrowRecordFavoriteStatus.put(record.getBookId(), isFavorite);
                }
            }

            // ⭐⭐⭐ 关键代码：设置收藏状态映射 ⭐⭐⭐
            request.setAttribute("borrowRecordFavoriteStatus", borrowRecordFavoriteStatus);

            // 添加真实的借阅记录
            request.setAttribute("currentBorrowRecords", currentBorrowRecords);

            // 转发到用户仪表板
            request.getRequestDispatcher("user/dashboard.jsp").forward(request, response);
        }
    }
}
